package top.fangw.oauth.model;

import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import top.fangw.common.api.vo.UserVo;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @description: 登录鉴权的用户
 * @author: fgw
 * @createDate: 2023/11/22
 */
@Data
public class OauthUser implements UserDetails {

    private String id;

    private String userName;

    private String password;

    private List<SimpleGrantedAuthority> grantedAuthorityList;


    public OauthUser(SysUserEntity sysUserEntity){
        this.id = sysUserEntity.getId().toString();
        this.userName = sysUserEntity.getName();
        this.password = sysUserEntity.getPassword();
        this.grantedAuthorityList = Arrays.asList("USER").stream().map(SimpleGrantedAuthority::new).collect(Collectors.toList());
    }

    public OauthUser(UserVo userVo){
        this.id = userVo.getId();
        this.userName = userVo.getName();
        this.password = userVo.getPassword();
        this.grantedAuthorityList = Arrays.asList("USER").stream().map(SimpleGrantedAuthority::new).collect(Collectors.toList());
    }

    /**
     * 设置用户权限
     */
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return this.grantedAuthorityList;
    }

    @Override
    public String getPassword() {
        return this.password;
    }

    @Override
    public String getUsername() {
        return this.userName;
    }

    /**
     * 获取账号是否 未过期
     * @return boolean
     */
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    /**
     * //判断账号是否 未锁定
     * @return boolean
     */
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    /**
     * 判断用户凭证是否 未过期
     * @return boolean
     */
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    /**
     * 判断账号是否可用
     * @return boolean
     */
    @Override
    public boolean isEnabled() {
        return true;
    }
}
